﻿using LiveCharts;
using LiveCharts.Defaults;

namespace Company.Shell.Extensions
{
    public static class ObservablePointExtension
    {
        /// <summary>
        /// 根据X得到Y
        /// </summary>
        /// <param name="points"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        public static ushort GetValue(this ChartValues<ObservablePoint> points,double x)
        {
            int index = 0;//下标
            foreach (var item in points)
            {
                if (x < item.X)
                {
                    break;
                }
                index++;
            }

            if (index >= points.Count)
            {
                index = points.Count - 1;
            }

            //y=kx+b 
            //比如说（1，2），（3，4）,k=(4-2)/3-1 k=(y2-y1)/(x2-x1)
            var left = points[index - 1];
            var right = points[index];
            var k=(right.Y-left.Y)/(right.X-left.X);
            var b = left.Y - k * left.X;
            var y = k * x + b;
            return(ushort)(y*10);
        }
    }
}
